/*
Copyright (c) 2009, Young Digital Planet SA
All rights reserved.

* Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this 
  list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, 
  this list of conditions and the following disclaimer in the documentation 
  and/or other materials provided with the distribution.
* Neither the name of the Young Digital Planet SA nor the names of its 
  contributors may be used to endorse or promote products derived from this 
  software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
package pl.ydp.utils
{
	import flexunit.framework.TestCase;
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import flash.events.Event;
	import flexunit.framework.Assert;

	public class XMLUtilTest extends TestCase
	{
		/**
		 * Testuję czy dokument jest prawidłowo normalizowany do
		 * parsowania bez ignorowania whitespace'ów
		 */
		protected function testWhiteSpace( event:Event ):void {
			var xmlString:String =
			"<?xml version='1.0' encoding='UTF-8'?>\r\n"+
"<assessmentItem version='1.0' ><properties>\r\n"+
"<property name=\"width\">300</property>\r\n"+
"<property name=\"height\">400</property>\r\n"+
"</properties>\r\n"+
"<layout>\r\n"+
"<module  name='pageInPage1' top='_root:top+32' left='_root:left+16' right='_root:right-32'  />\r\n"+
"</layout>\r\n"+
"<itemBody>\r\n"+
"<pageInPage id='pageInPage1' >\r\n"+
"</pageInPage>\r\n"+
"</itemBody>\r\n"+
"</assessmentItem>";
			var st:Object = XML.settings();
			XML.ignoreWhitespace = false;
			
			var normalizedXML:String = XMLUtil.normalizeWhitespaceXML(xmlString);
			var root:XML = new XML( normalizedXML );
			
			XML.setSettings( st );
		}
		
		public function testXmlChildren2html():void{
			var node:XML = <section xmlns="http://www.ydp.com.pl" id="1" class="dumClass">
								TEST test   test<b> bold</b> <i>italic</i>
								test <p class="dumClass1" id="2"> paragraf  asdd </p> chars &"fdfas"
								  <br/> nline 
							</section>;
			var nodeString:String = "TEST test test<b>bold</b><i>italic</i>test" + 
									"<p class=\"dumClass1\" id=\"2\">paragraf asdd</p>" + 
									"chars &amp;&quot;fdfas&quot;<br/>nline";
			
			Assert.assertEquals(nodeString, XMLUtil.xmlChildren2html(node));
		}
		
		public function testXml2html():void{
			var node:XML = <section xmlns="http://www.ydp.com.pl" id="1" class="dumClass">
								TEST test   test<b> bold</b> <i>italic</i>
								test <p class="dumClass1" id="2"> paragraf  asdd </p> chars &"fdfas"
								  <br/> nline 
							</section>;
			var nodeString:String = "<section id=\"1\" class=\"dumClass\">TEST test test" + 
									"<b>bold</b><i>italic</i>test<p class=\"dumClass1\" id=\"2\">paragraf asdd</p>" + 
									"chars &amp;&quot;fdfas&quot;<br/>nline</section>";
			
			Assert.assertEquals(nodeString, XMLUtil.xml2html(node));
		}
		
		public function testGetDescendantIndex():void{
			var node:XML = <section>
								<item/>
								<item>
									<itemChild1/>
									<itemChild1>
										<itemChildChild1/>
										<itemChildChild1>
											<child/>
										</itemChildChild1>
									</itemChild1>
									<itemChild2/>
								</item>
							</section>;
							
			Assert.assertEquals(-1, XMLUtil.getDescendantIndex(node));
			Assert.assertEquals(2, XMLUtil.getDescendantIndex(node.item.itemChild1[0]));
			Assert.assertEquals(3, XMLUtil.getDescendantIndex(node.item.itemChild1[1]));
			Assert.assertEquals(4, XMLUtil.getDescendantIndex(node.item.itemChild1.itemChildChild1[0]));
			Assert.assertEquals(6, XMLUtil.getDescendantIndex(XML(node.item.itemChild1.itemChildChild1.child)));
			Assert.assertEquals(7, XMLUtil.getDescendantIndex(XML(node.item.itemChild2)));
		}
		
		public function testCreateNodeID():void{
			var node:XML = <section>
								<item/>
								<item id="item">
									<itemChild1/>
									<itemChild1 id="itemChild1">
										<itemChildChild1/>
										<itemChildChild1>
											<child/>
										</itemChildChild1>
									</itemChild1>
									<itemChild2/>
								</item>
							</section>;
							
			Assert.assertEquals("section_root_0", XMLUtil.createNodeID(node));
			Assert.assertEquals("itemChild1_item_0", XMLUtil.createNodeID(node.item.itemChild1[0]));
			Assert.assertEquals("itemChild1_itemChild1_0", XMLUtil.createNodeID(node.item.itemChild1[1]));
			Assert.assertEquals("itemChildChild1_itemChild1_0", XMLUtil.createNodeID(node.item.itemChild1.itemChildChild1[0]));
			Assert.assertEquals("itemChildChild1_itemChild1_1", XMLUtil.createNodeID(node.item.itemChild1.itemChildChild1[1]));
			Assert.assertEquals("child_itemChild1_0", XMLUtil.createNodeID(XML(node.item.itemChild1.itemChildChild1.child)));
			Assert.assertEquals("itemChild2_item_0", XMLUtil.createNodeID(XML(node.item.itemChild2)));
			
			Assert.assertEquals("section_root_0", XMLUtil.createNodeID(node, true));
			Assert.assertEquals("itemChild1_item_0_2", XMLUtil.createNodeID(node.item.itemChild1[0], true));
			Assert.assertEquals("itemChild1_itemChild1_0_3", XMLUtil.createNodeID(node.item.itemChild1[1], true));
			Assert.assertEquals("itemChildChild1_itemChild1_0_4", XMLUtil.createNodeID(node.item.itemChild1.itemChildChild1[0], true));
			Assert.assertEquals("itemChildChild1_itemChild1_1_5", XMLUtil.createNodeID(node.item.itemChild1.itemChildChild1[1], true));
			Assert.assertEquals("child_itemChild1_0_6", XMLUtil.createNodeID(XML(node.item.itemChild1.itemChildChild1.child), true));
			Assert.assertEquals("itemChild2_item_0_7", XMLUtil.createNodeID(XML(node.item.itemChild2), true));
		}
		
		public function testCodeXMLChars():void{
			var inputString:String = "  \r\t\n test \r\t\ntest     test &\"'<>\uffa0 test   ";
			Assert.assertEquals(" test test test &amp;&quot;&apos;&lt;&gt;\u00a0 test ", XMLUtil.codeXMLChars(inputString));
		}
		
		public function testEncodeXMLChars():void{
			Assert.assertEquals("&amp;&quot;&apos;&lt;&gt;\u00a0", XMLUtil.encodeXMLChars("&\"'<>\uffa0"));
			Assert.assertEquals("&amp;&quot;&amp;&amp;&gt;", XMLUtil.encodeXMLChars("&\"&&>"));
		}
		
		public function testDecodeXMLChars():void{
			Assert.assertEquals("&\"'<>\uffa0", XMLUtil.decodeXMLChars("&amp;&quot;&apos;&lt;&gt;\u00a0"));
			Assert.assertEquals("&\"&&>", XMLUtil.decodeXMLChars("&amp;&quot;&amp;&amp;&gt;"));
		}
		
		public function testPathBase():void{
			var node:XML = <node/>;
			
			Assert.assertNull("Path was not set", XMLUtil.getPathBase(node));
			
			XMLUtil.setPathBase(node, new SimplePathBase2("http://server/page2/"));
			Assert.assertEquals("Path was set", "http://server/page2/", XMLUtil.getPathBase(node).resolvePath(""));
		}
		
		public function testGetRoot():void{
			var node:XML = <section>
								<item/>
								<item>
									<itemChild1/>
									<itemChild1>
										<itemChildChild1/>
										<itemChildChild1>
											<child/>
										</itemChildChild1>
									</itemChild1>
									<itemChild2/>
								</item>
							</section>;
							
			Assert.assertEquals(node, XMLUtil.getRoot(node.item[0]));
			Assert.assertEquals(node, XMLUtil.getRoot(node.item.itemChild1[0]));
			Assert.assertEquals(node, XMLUtil.getRoot(node.item.itemChild1[1]));
			Assert.assertEquals(node, XMLUtil.getRoot(node.item.itemChild1.itemChildChild1[0]));
			Assert.assertEquals(node, XMLUtil.getRoot(XML(node.item.itemChild1.itemChildChild1.child)));
			Assert.assertEquals(node, XMLUtil.getRoot(XML(node.item.itemChild2)));
		}
		
		public function testAddClass():void {
			var node:XML = <node />;
			XMLUtil.addClass( node, "cls" );
			Assert.assertEquals( "Class for empty attr not set", "cls", String(node.attribute("class")) );
			
			node = <node class="abc"/>;
			XMLUtil.addClass( node, "cls" );
			Assert.assertEquals( "Class for not set", "abc cls", String(node.attribute("class")) );
			
			node = <node class="abc def"/>;
			XMLUtil.addClass( node, "cls" );
			Assert.assertEquals( "Class for not set", "abc def cls", String(node.attribute("class")) );
			
			node = <node class="cls abc"/>;
			XMLUtil.addClass( node, "cls" );
			Assert.assertEquals( "Class not set", "cls abc", String(node.attribute("class")) );
		}
		
		public function testAppendXML():void{
			var node1:XML = <section>
								<item style="style1" id="2">
									<child> text target </child>
								</item>
							</section>;
			var node2:XML = <section>
							<item id="1">
								<child>text</child>
							</item></section>;
			var node3:XML = <item>
								<child> child3 </child>
							</item>;
			
			//oczekiwane wyniki
			var nodeOut1:XML = <section>
								  <item style="style1" id="1">
								    <child>text target</child>
								  </item>
								</section>;
			var nodeOut2:XML = <section>
								  <item style="style1" id="1">
								    <child>text target</child>
								  </item>
								  <child>child3</child>
								</section>


			XMLUtil.appendXML(node1, node2);
			Assert.assertEquals("Overrides node", nodeOut1, node1);
			
			XMLUtil.appendXML(node1, node3);
			Assert.assertEquals("Appends node to node list", nodeOut2, node1);
		}
	}
}